package com.kye.express.business.jd.order_detail;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.jd.order_detail.model.JDOrderDetailModel;
import com.kye.express.business.waybill.waybill_detail.RouterStatusTimeLineView;
import com.kye.express.databinding.ActivityIntegralOrderDetailJdRouterBinding;
import com.kye.express.view.ToastManager;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.model.bean.jd.order_details.JDOrderRouterInfoBean;
import com.kye.model.bean.user.User;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 24/01/2018
 * @updated : 24/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDOrderDetailRouterInfoActivity extends BaseToolbarActivity implements JDOrderDetailRouterInfoConstract.JDOrderDetailRouterInfoView {

    private ActivityIntegralOrderDetailJdRouterBinding binding;
    private JDOrderDetailRouterInfoConstract.IntegralOrderJDRouterInfoPresenter presenter;

    private JDOrderDetailModel orderDetailModel;
    private ArrayList<JDOrderRouterInfoBean> routerInfoBeans;
    private User user;

    public static void startActivity(Context context, JDOrderDetailModel orderDetailModel, ArrayList<JDOrderRouterInfoBean> routerInfoBeans) {
        Intent intent = new Intent(context, JDOrderDetailRouterInfoActivity.class);
        intent.putExtra(JDOrderDetailModel.class.getName(), orderDetailModel);
        intent.putParcelableArrayListExtra(JDOrderRouterInfoBean.class.getName() + "s", routerInfoBeans);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            orderDetailModel = getIntent().getParcelableExtra(JDOrderDetailModel.class.getName());
            routerInfoBeans = getIntent().getParcelableArrayListExtra(JDOrderRouterInfoBean.class.getName() + "s");
        } else {
            orderDetailModel = savedInstanceState.getParcelable(JDOrderDetailModel.class.getName());
            routerInfoBeans = savedInstanceState.getParcelableArrayList(JDOrderRouterInfoBean.class.getName() + "s");
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_integral_order_detail_jd_router);
        presenter = new JDOrderDetailRouterInfoConstract.IntegralOrderJDRouterInfoPresenter();
        initToolbat(savedInstanceState);
        initViews(savedInstanceState);
        initEvents(savedInstanceState);
        initData(savedInstanceState);
    }

    private void initToolbat(Bundle savedInstanceState) {
        setUpToolbar(binding.toolbar);
    }

    private void initViews(Bundle savedInstanceState) {

    }

    private void initEvents(Bundle savedInstanceState) {
        RxView.clicks(binding.llErrorNet.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(binding.llErrorServer.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        binding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {

            }
        });
    }

    private void initData(Bundle savedInstanceState) {
        presenter.bind(this);
        binding.statusLayout.showLoadingView();
        presenter.initRouterData(routerInfoBeans);
    }

    @Override
    public void onUser(User user) {
        this.user = user;
    }

    @Override
    public void initRouterView(List<RouterStatusTimeLineView.RouterStatusTimeLineModel> routerStatusTimeLineModels) {
        binding.statusLayout.showContentView();
        binding.setData(orderDetailModel);
        binding.orderRouter.setRouterStatusDataList(routerStatusTimeLineModels);
        binding.orderRouter.setFinishedForView(true);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(JDOrderDetailModel.class.getName(), orderDetailModel);
        outState.putParcelableArrayList(JDOrderRouterInfoBean.class.getName() + "s", routerInfoBeans);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_order_jd_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_contact:
                ToastManager.showCenterForShort("联系客服");
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
